package com.hxd.otc.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.hxd.otc.R;

import java.io.File;

import de.hdodenhof.circleimageview.CircleImageView;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;

/**
 * 项  目 :  OrderSupervisor
 * 包  名 :  com.hengxunda.ordersupervisor.utils
 * 类  名 :  GlideUtils
 * 作  者 :  胡庆岭
 * 时  间 :  2018/4/25 14:13
 * 描  述 :  ${TODO} 图片加载工具类
 */
public class GlideUtils {

    private static RequestOptions defaultOptions(@DrawableRes int placeholderRes, @DrawableRes int errorRes) {
        return new RequestOptions().centerCrop().placeholder(placeholderRes).error(errorRes).diskCacheStrategy(DiskCacheStrategy.ALL).override(Target.SIZE_ORIGINAL);
    }

    public static void setImage(@NonNull String url, @NonNull ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop();
        Glide.with(UIUtils.getContext()).load(url).apply(requestOptions).into(imageView);
    }

    public static void setImage(@NonNull Context context, @NonNull String url, @NonNull ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop();
        Glide.with(context).load(url).apply(requestOptions).into(imageView);
    }

    public static void setImage(@NonNull Context context, @DrawableRes int imageRes, @NonNull ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop();
        Glide.with(context).load(imageRes).apply(requestOptions).into(imageView);
    }

    public static void setImage(@NonNull String url, @DrawableRes int placeholderRes, @DrawableRes int errorRes, @NonNull ImageView imageView) {
        Glide.with(UIUtils.getContext()).load(url).apply(GlideUtils.defaultOptions(placeholderRes, errorRes)).into(imageView);
    }

    public static void setImage(@NonNull File imageFile, @DrawableRes int placeholderRes, @DrawableRes int errorRes, @NonNull ImageView imageView) {
        Glide.with(UIUtils.getContext()).load(imageFile).apply(GlideUtils.defaultOptions(placeholderRes, errorRes)).into(imageView);
    }

    public static void setImage(@NonNull Activity activity, @NonNull String url, @DrawableRes int placeholderRes, @DrawableRes int errorRes, @NonNull ImageView imageView) {
        Glide.with(activity).load(url).apply(GlideUtils.defaultOptions(placeholderRes, errorRes)).into(imageView);
    }

    public static void setImage(@NonNull Fragment fragment, @NonNull String url, @DrawableRes int placeholderRes, @DrawableRes int errorRes, @NonNull ImageView imageView) {
        Glide.with(fragment).load(url).apply(GlideUtils.defaultOptions(placeholderRes, errorRes)).into(imageView);
    }

    public static void setImage(@NonNull Context context, @NonNull String url, @DrawableRes int placeholderRes, @DrawableRes int errorRes, @NonNull ImageView imageView) {
        Glide.with(context).load(url).apply(GlideUtils.defaultOptions(placeholderRes, errorRes)).into(imageView);
    }

    public static void setImage(@NonNull View view, @NonNull String url, @DrawableRes int placeholderRes, @DrawableRes int errorRes, @NonNull ImageView imageView) {
        Glide.with(view).load(url).apply(GlideUtils.defaultOptions(placeholderRes, errorRes)).into(imageView);
    }

    /**
     * 设置一个圆形图片
     *
     * @param url            图片路径
     * @param imageView      ImageView
     * @param placeholderRes 预览图id
     * @param errorRes       错误图id
     */
    public static void setCircleImage(@NonNull String url, @NonNull ImageView imageView, @DrawableRes int placeholderRes, @DrawableRes int errorRes) {
        RequestOptions requestOptions = defaultOptions(placeholderRes, errorRes).circleCrop().dontAnimate();
        Glide.with(UIUtils.getContext()).load(url).apply(requestOptions).into(imageView);
    }

    public static void loadCenterCrop(Context context, String url, ImageView view) {

        Glide.with(context)
                .load(url)
                .transition(withCrossFade())
                .apply(new RequestOptions().centerCrop())
                .into(view);
    }

    /**
     * 带加载异常图片
     *
     * @param context
     * @param url
     * @param view
     * @param errorResId
     */
    public static void loadCenterCrop(Context context, String url, ImageView view, int errorResId) {

        Glide.with(context)
                .load(url)
                .transition(withCrossFade())
                .apply(new RequestOptions().centerCrop().error(errorResId))
                .into(view);
    }

    public static void loadCenterCrop(Fragment fragment, String url, ImageView view, int errorResId) {

        Glide.with(fragment)
                .load(url)
                .transition(withCrossFade())
                .apply(new RequestOptions().centerCrop().error(errorResId))
                .into(view);
    }

    /**
     * 带监听处理
     *
     * @param context
     * @param url
     * @param view
     * @param listener
     */
    public static void loadCenterCrop(Context context, String url, ImageView view, RequestListener listener) {
        Glide.with(context)
                .load(url)
                .transition(withCrossFade())
                .apply(new RequestOptions().centerCrop())
                .listener(listener)
                .into(view);
    }

    /**
     * 加载头图片
     *
     * @param mContext
     * @param path
     * @param mImageView
     */
    public static void loadHeadImageView(Context mContext, String path, CircleImageView mImageView, int errorResId, int placeholderRes) {
        Glide.with(mContext)
                .load(path)
                .apply(new RequestOptions().centerCrop().error(errorResId).placeholder(placeholderRes).dontAnimate())
                .into(mImageView);
    }

    /**
     * @param context
     * @param url
     * @param view
     */
    public static void loadNormal(Context context, String url, ImageView view) {
        Glide.with(context).load(url).into(view);
    }

    /**
     * @param context
     * @param url
     * @param view
     */
    public static void loadSystemMessage(Context context, String url, ImageView view) {

        Glide.with(context).load(url).apply(GlideUtils.defaultOptions(R.mipmap.icon_wallet_header_bg_of_otc, R.mipmap.icon_wallet_header_bg_of_otc)).into(view);
    }

    /**
     * 加载圆角图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadConvenienceBanner(Context context, int cornerValue, String url, ImageView imageView) {
        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(DataUtils.dp2px(context, cornerValue));
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        Glide.with(context).load(url).apply(options).into(imageView);
    }

    /**
     * 获取一张Bitmap
     *
     * @param url                      路径
     * @param onGlideLoadImageListener 接口
     */
    @SuppressLint("CheckResult")
    public static void loadBitmap(@NonNull final String url, final OnGlideLoadImageListener onGlideLoadImageListener) {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext(url);
            }
        }).map(new Function<String, Bitmap>() {
            @Override
            public Bitmap apply(String s) throws Exception {
                return Glide.with(UIUtils.getContext()).asBitmap().load(url).submit().get();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Bitmap>() {
                    @Override
                    public void accept(Bitmap bitmap) throws Exception {
                        if (onGlideLoadImageListener != null && bitmap != null && bitmap.getByteCount() > 0) {
                            onGlideLoadImageListener.onLoadBitmap(bitmap);
                        }
                    }
                });
    }

    public interface OnGlideLoadImageListener {
        void onLoadBitmap(Bitmap bitmap);
    }
}
